//
//  PopVIewAnimator.swift
//  WeiBoSwift
//
//  Created by Jacky Sun on 16/4/25.
//  Copyright © 2016年 syf. All rights reserved.
//

import UIKit

let  PopUpViewAnimatorWillShow = "willShow"
let  PopUpViewAnimatorWillDismiss = "willDismiss"


class PopVIewAnimator: NSObject,  UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
    //
    var isPresent: Bool = false
    var presentFrame = CGRectZero
    
    
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?{
        
        let popVC = PopUpPresentationController(presentedViewController: presented, presentingViewController: presenting)
        popVC.popViewFrame  = presentFrame
        return  popVC
    }
    
    // MARK: - 只要实现了一下方法, 那么系统自带的默认动画就没有了, "所有"东西都需要程序员自己来实现
    /**
     告诉系统谁来负责Modal的展现动画
     
     :param: presented  被展现视图
     :param: presenting 发起的视图
     :returns: 谁来负责
     */
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        NSNotificationCenter.defaultCenter().postNotificationName(PopUpViewAnimatorWillShow, object: self, userInfo: nil)
        return self
    }
    
    /**
     告诉系统谁来负责Modal的消失动画
     
     :param: dismissed 被关闭的视图
     :returns: 谁来负责
     */
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        NSNotificationCenter.defaultCenter().postNotificationName(PopUpViewAnimatorWillDismiss, object: self, userInfo: nil)
        return self
    }
    
    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        if(isPresent)
        {
            let tabView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            tabView.transform = CGAffineTransformMakeScale(1.0, 0.0)
            
            // 一定将视图添加到容器里面
            transitionContext.containerView()?.addSubview(tabView)
            // 设置锚点
            tabView.layer.anchorPoint = CGPoint(x: 0.5, y: 0.0)
            
            UIView.animateWithDuration(0.5, animations: {
                tabView.transform = CGAffineTransformIdentity
                }, completion: { (_) in
                    transitionContext.completeTransition(true)
            })
        }
        else
        {
            let tabView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
            
            UIView.animateWithDuration(0.2, animations: {
                //// 注意:由于CGFloat是不准确的, 所以如果写0.0会没有动画
                
                tabView.transform = CGAffineTransformMakeScale(1.0, 0.000001)
                }, completion: { (_) in
                    transitionContext.completeTransition(true)
            })
        }
        
    }

    
}
